﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace PlantsVsZombies
{
    public  class CharacterManager : EntityManager
    {
        private Level level;

        
        private Color color = Color.Green;
        private int _nColor = 2;
        private int _delay = 0;
        private int _iColor = 0;
        private Sprite _gameOver;
        private float _normalDelay = 16f;

        public override void LoadContent(ContentManager contentManager)
        {
            MyContent.content = contentManager;
            List<Ghost> ghost = new List<Ghost>();
            ghost.Add(new Zombie(contentManager, new Vector2(800, 200f), 1));
            ghost.Add(new Zombie(contentManager, new Vector2(800, 200f), 3));
            ghost.Add(new Zombie(contentManager, new Vector2(800, 200f), 4));
            ghost.Add(new Zombie(contentManager, new Vector2(800, 200f), 5));
            ghost.Add(new Zombie(contentManager, new Vector2(800, 200f), 7));
            ghost.Add(new Zombie(contentManager, new Vector2(800, 200f), 20));
            
            //ghost.Add(new Zombie(contentManager, new Vector2(800, 300f), 20));
            //ghost.Add(new Zombie(contentManager, new Vector2(800, 400f), 18));
            //ghost.Add(new Zombie(contentManager, new Vector2(800, 500f), 21));

            List<Vehicle> vehicles = new List<Vehicle>();
            vehicles.Add(new Vehicle(new Vector2(-30, 100f)));
            //vehicles.Add(new Vehicle(new Vector2(-30, 200f)));
            //vehicles.Add(new Vehicle(new Vector2(-30, 300f)));
            //vehicles.Add(new Vehicle(new Vector2(-30, 400f)));
            //vehicles.Add(new Vehicle(new Vector2(-30, 500f)));
            level = new Level(ghost.Count,ghost, contentManager,vehicles);

            int nPlant = 4;
            Plant[] plant = new Plant[nPlant];
            plant[0] = new SunFlower(contentManager,Vector2.Zero);
            plant[1] = new SunFlower(contentManager,Vector2.Zero);
            plant[2] = new Peashooter(contentManager,Vector2.Zero);
            plant[3] = new Peashooter(contentManager, Vector2.Zero);
            level.LoadPlantTemplete(plant, nPlant);

            LoadGameOver(contentManager);
        }

        private void LoadGameOver(ContentManager contentManager)
        {
            _gameOver = new Sprite(contentManager, @"Image\GameOver\gameoverlogo",Vector2.Zero,Vector2.Zero);
        }
        public override void Update(ref GameTime gameTime, ref GameStates gameStates)
        {
            switch(gameStates)
            {
                case GameStates.Playing:
                    {
                        level.Update(gameTime);
                        if (level.GameOver)
                            gameStates = GameStates.GameOver;
                        break; 
                    }
                case GameStates.GameOver:
                    {
                        //var delta = (int)(gameTime.TotalGameTime.Milliseconds / _normalDelay);
                        //if (_delay <= 8)
                        //    _delay++;
                        //else
                        //{
                        //    _iColor = delta % _nColor;
                        //    _delay = 0;
                        //}

                        //switch (_iColor)
                        //{
                        //    case 0:
                        //        color = Color.White;
                        //        break;
                        //    case 1:
                        //        color = Color.Red;
                        //        break;
                        //}
                        break;
                    }
               
            }

            
        }

        public override void Draw(SpriteBatch spriteBatch, GameStates gameStates)
        {
            
            level.Draw(spriteBatch);
             if(gameStates ==  GameStates.GameOver)
                    {
                       _gameOver.Draw(spriteBatch,color);
                    }
           
        }
       
    }
}
